<style>
  .layui-form-label {
    width: 100px;
  }

  .layui-input-block {
    margin-left: 130px;
  }

  .layui-form-select {
    width: 30%;
    float: left;
    margin-right: 10px;
  }

  .icon-del {
    font-size: 34px;
    color: red;
    float: left;
    cursor: pointer;
  }
</style>
<!-- 正文开始 -->
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-card-body">
      <!-- 头部操作栏 -->
      <div class="layui-form lay-header">
        <div class="toolbar">
          <div class="layui-form-item lay-query-content">
            <div class="lay-query-box">
              <div class="query-lt">
                <div class="layui-inline">
                  <label class="layui-form-label w-auto">名称：</label>
                  <div class="layui-input-inline">
                    <input name="name" class="layui-input" type="text" placeholder="请输入名称" />
                  </div>
                </div>
                <div class="layui-inline">
                  <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit>查询</button>
                  <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                </div>
              </div>
              <div class="query-rt"></div>
            </div>
          </div>
        </div>
        <div class="lay-btn-rows">
          <div class="lay-btn-box">
            <div class="lay-btn-lt">
              <button id="productWorkmanshipBtnAdd" class="layui-btn icon-btn color-reseda">添加</button>
            </div>
            <div class="lay-btn-rt"></div>
          </div>
        </div>
      </div>
      <!-- 表格 -->
      <table class="layui-table" id="systemMsgCategoryTable" lay-filter="systemMsgCategoryTable"></table>
    </div>
  </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="systemMsgCategoryTableBar">
  <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">修改</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script type="text/html" id="jobsBar">
  <a class="layui-btn layui-btn-xs" lay-event="view">点击查看</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="systemMsgCategoryForm">
  <form lay-filter="systemMsgCategoryForm" class="layui-form open-form">
    <div class="open-model">
      <div class="open-auto">
        <input name="id" type="hidden" />
        <div class="layui-form-item" id="PID">
          <label class="layui-form-label"><span style="color: red">*</span>所属科目分类</label>
          <input name="subId" type="hidden" lay-verify="subId" />
          <div class="layui-input-block" id="selectCategory"></div>
        </div>

        <div class="layui-form-item">
          <label class="layui-form-label"><span style="color: red">*</span>科目名称</label>
          <div class="layui-input-block">
            <input name="name" placeholder="请输入科目名称" type="text" class="layui-input" lay-verify="required" required />
          </div>
        </div>

        <div class="layui-form-item" id="applyCompany">
          <label class="layui-form-label"><span style="color: red">*</span>适用公司</label>
          <div class="layui-input-block">
            <input name="selId" type="hidden" />
            <select name="companyInfoId" disabled type="text" class="layui-input" lay-filter="getCompanyInfo" lay-verify="required" required></select>
            <div id="departmentId0" style="float: left;width: 30%;margin-right: 10px;"></div>
            <input type="hidden" name="departmentId" />
            <select name="roleId" type="text" class="layui-input" lay-verify="required" required></select>
          </div>
        </div>

        <div class="layui-form-item">
          <label class="layui-form-label"></label>
          <div class="layui-input-block">
            <div class="layui-btn" type="button" id="addCompany">添加企业</div>
          </div>
        </div>

        <div class="layui-form-item">
          <label class="layui-form-label">描述</label>
          <div class="layui-input-block">
            <textarea id="remark" name="remark" placeholder="请输入描述" class="layui-textarea" />
          </div>
        </div>

        <div class="layui-form-item">
          <label class="layui-form-label"><span style="color: red">*</span>适用客户类型</label>
          <div class="layui-input-block">
            <input name="customerType" type="checkbox" title="一般客户" checked="checked" value="0" />
            <input name="customerType" type="checkbox" title="核心客户" value="1" />
            <input name="customerType" type="checkbox" title="战略客户" value="2" />
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">是否启用</label>
          <div class="layui-input-block">
            <input name="isDeleted" type="checkbox" lay-skin="switch" value="1" checked="false" lay-text="启用|停用" />
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">排序</label>
          <div class="layui-input-block">
            <input name="sortNumber" placeholder="请输入排序" type="text" class="layui-input" lay-verify="" required />
          </div>
        </div>
      </div>
      <div class="layui-form-item text-right">
        <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        <button class="layui-btn" lay-filter="systemMsgCategoryFormSubmit" lay-submit>保存</button>
      </div>
    </div>
  </form>
</script>

<script type="text/html" id="childForm">
  <form lay-filter="systemMsgCategoryForm" class="layui-form model-form">
    <input name="id" type="hidden" />
    <input name="subId" type="hidden" />
    <div class="layui-form-item">
      <label class="layui-form-label">名称</label>
      <div class="layui-input-block">
        <input name="name" placeholder="请输入名称" type="text" class="layui-input" lay-verify="required" required />
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">描述</label>
      <div class="layui-input-block">
        <textarea name="remark" placeholder="请输入描述" class="layui-textarea" />
      </div>
    </div>

    <div class="layui-form-item">
      <label class="layui-form-label">排序</label>
      <div class="layui-input-block">
        <input name="sortNumber" placeholder="请输入排序" type="text" class="layui-input" lay-verify="" required />
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">是否启用</label>
      <div class="layui-input-block">
        <input name="isDeleted" type="checkbox" lay-skin="switch" value="1" checked="false" lay-text="启用|停用" />
      </div>
    </div>

    <div class="layui-form-item text-right">
      <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
      <button class="layui-btn" lay-filter="childFormSubmit" lay-submit>保存</button>
    </div>
  </form>
</script>

<!-- 责任岗位弹窗 -->
<script type="text/html" id="jobsView">
  <div class="layui-card">
    <div class="layui-card-body">
      <!-- 表格 -->
      <div class="table-bor">
        <table class="layui-table" id="jobsViewTable" lay-filter="jobsViewTable"></table>
      </div>
    </div>
  </div>
</script>

<!-- js部分 -->
<script>
  layui.use(["layer", "form", "table", "laydate", "admin", "laytpl", "tableTreeDj", "config"], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var config = layui.config;
    var admin = layui.admin;
    var tableTree = layui.tableTreeDj;
    var TreeData = [];
    var i = 0;
    var customerTypeArr = ["一般客户", "核心客户", "战略客户"];

    // 渲染表格
    var insTb = table.render({
      id: "#systemMsgCategoryTable",
      elem: "#systemMsgCategoryTable",
      url: config.base + "enterprise/sell/admin/customerDueSubject/list",
      toolbar: true,
      defaultToolbar: ["filter"],
      page: true,
      cellMinWidth: 100,
      cols: [
        [
          { type: "numbers" },
          { field: "name", align: "center", title: "科目名称" },
          { field: "subName", align: "center", title: "所属科目分类" },
          { align: "center", title: "责任岗位", toolbar: "#jobsBar" },
          {
            field: "customerType",
            align: "center",
            title: "适用客户类型",
            templet: function (d) {
              var customerType = d.customerType;
              if (customerType) {
                if (customerType.indexOf(",") != -1) {
                  var types = customerType.split(",");
                  for (var j = 0; j < types.length; j++) {
                    if (j == 0) {
                      customerType = customerTypeArr[types[j]];
                    } else {
                      customerType += "," + customerTypeArr[types[j]];
                    }
                  }
                } else {
                  customerType = customerTypeArr[customerType];
                }
              }
              return customerType;
            }
          },
          { field: "remark", align: "center", title: "描述" },
          {
            field: "isDeleted",
            align: "center",
            title: "是否启用",
            templet: function (d) {
              if (d.isDeleted) {
                return (
                  '<input name="isDeleted" type="checkbox" lay-skin="switch" value="' + d.id + '" checked="' + d.isDeleted + '" lay-filter="isDeleted"  lay-text="启用|停用"/>'
                );
              } else {
                return '<input name="isDeleted" type="checkbox" lay-skin="switch" value="' + d.id + '" lay-filter="isDeleted"   lay-text="启用|停用"/>';
              }
            }
          },
          { field: "creatorName", align: "center", title: "创建人" },
          { field: "createTime", align: "center", title: "创建时间" },
          { align: "center", toolbar: "#systemMsgCategoryTableBar", title: "操作", width: 210 }
        ]
      ]
    });

    // 工具条点击事件
    table.on("tool(systemMsgCategoryTable)", function (obj) {
      var data = obj.data;
      if (obj.event === "edit") {
        //修改
        showEditModel(data, "systemMsgCategoryForm", "systemMsgCategoryFormSubmit");
      } else if (obj.event === "del") {
        // 删除
        doDelete(obj);
      } else if (obj.event === "view") {
        // 点击查看责任岗位
        showViewModel(data);
      }
    });

    //监听排序
    table.on("sort(systemMsgCategoryTable)", function (obj) {
      table.reload("systemMsgCategoryTable", {
        initSort: obj,
        where: {
          sort: obj.field,
          order: obj.type
        }
      });
    });

    // 搜索
    form.on("submit(formSubSearchLog)", function (data) {
      insTb.reload({ where: data.field, page: { curr: 1 } }, "data");
    });

    // 重置搜索
    form.on("submit(resets)", (data) => {
      $(".lay-header").find("select").val("");
      $(".lay-header").find("input").val("");
      let field = data.field;
      for (let key in field) {
        field[key] = "";
      }
      insTb.reload({ where: field, page: { curr: 1 } }, "data");
    });

    form.on("switch(isDeleted)", function (obj) {
      if (obj.elem.checked) {
        enable(obj.value);
      } else {
        disable(obj.value);
      }
    });

    //是否启用
    function enable(id) {
      admin.req(
        "enterprise/sell/admin/customerDueSubject/enable",
        { id: id },
        function (res) {
          if (res.code == 200) {
            layer.msg(res.msg, { icon: 1 });
            insTb.reload("systemMsgCategoryTable");
          } else {
            layer.msg(res.msg, { icon: 2 });
          }
        },
        "POST"
      );
    }

    //是否停用
    function disable(id) {
      admin.req(
        "enterprise/sell/admin/customerDueSubject/disable",
        { id: id },
        function (res) {
          if (res.code == 200) {
            layer.msg(res.msg, { icon: 1 });
            insTb.reload("systemMsgCategoryTable");
          } else {
            layer.msg(res.msg, { icon: 2 });
          }
        },
        "POST"
      );
    }

    // 添加按钮点击事件
    $("#productWorkmanshipBtnAdd").click(function () {
      showEditModel(undefined, "systemMsgCategoryForm", "systemMsgCategoryFormSubmit");
    });

    // 显示编辑弹窗
    function showEditModel(data, formId, submitButton) {
      let finalCategoryIds = [];
      form.on("select(type)", function (data) {
        if (data.value === "text") {
          $("#btnAdd").hide();
        } else {
          $("#btnAdd").show();
        }
      });
      admin.open({
        type: 1,
        area: ["1000px", "80%"],
        offset: "65px",
        title: data ? "编辑尽调科目" : "添加尽调科目",
        content: $("#" + formId).html(),
        success: function () {
          //自定义验证规则
          form.verify({
            subId: function (value) {
              if (value == "") {
                return "请选择科目分类";
              }
            }
          });

          var getTemplateHtml = function (i, value, update) {
            var labelName = "";
            var valLen = $("form input[name='value']").length;
            var classNew = "";
            if (valLen == 0) {
              labelName = "参数值";
            } else {
              classNew = "newAdd";
            }
            var inputHtml = "";
            if (update) {
              inputHtml =
                '<input id="value' + i + '" name="paramValue" value="' + value + '" placeholder="请输入参数" type="text" class="layui-input" lay-verify="required" required/>';
            } else {
              inputHtml = '<input id="value' + i + '" name="paramValue" placeholder="请输入参数" type="text" class="layui-input" lay-verify="required" required/>';
            }
            console.log(inputHtml);
            var templateHtml =
              "" +
              '<div class="layui-form-item ' +
              classNew +
              '" >' +
              '<div class="layui-form-item">' +
              "<div >" +
              '<label class="layui-form-label" >' +
              labelName +
              "</label>" +
              '<div class="layui-input-inline">' +
              inputHtml +
              "</div>" +
              "</div>" +
              "<div >" +
              // '<div class="layui-input-inline">' +
              // '<input id="sort'+i+'" name="sort" placeholder="排序值" type="text" class="layui-input" lay-verify="sortNumber" required/>' +
              // '</div>' +
              "</div>";

            templateHtml +=
              '<div class="layui-input-inline">' +
              '<button id="btnDel' +
              i +
              '" class="layui-btn delBtn" lay-filter="btnDel' +
              i +
              '" style="margin-left: 20px;">删除</button>' +
              "</div>";

            templateHtml += "</div>" + "</div>";

            return templateHtml;
          };

          var addTemplateDiv = function (value, update) {
            var html = getTemplateHtml(i++, value, update);
            $("#btnAddDiv").prev().after(html);

            form.render();
            // btnCount++;
            form.render("select");

            $(".delBtn")
              .off("click")
              .on("click", function () {
                var btn = $(this);
                var btnId = btn[0].id;
                var btnNo = btnId.substring("btnDel".length, btnId.length);

                btn.parent().parent().parent().remove();
              });
          };

          if (formId != "childForm") {
            var json = {};
            if (data && data.subId) {
              json.selectId = data.subId;
            }

            admin.req(
              "enterprise/sell/admin/customerDueSubjectCategory/getTreeData",
              json,
              function (obj) {
                TreeData = obj.data;

                xmSelect.render({
                  el: "#selectCategory",
                  model: { label: { type: "text" } },
                  radio: true,
                  clickClose: true,
                  filterable: true,
                  tree: {
                    show: true,
                    strict: false,
                    /*expandedKeys: [-1],*/
                    expandedKeys: true
                  },
                  tips: "顶级分类",
                  height: "auto",
                  data() {
                    return TreeData;
                  },
                  on: function (data) {
                    //被选中的select的id,如果没有 -1
                    let subId = data.arr.length > 0 ? data.change[0].value : -1;
                    $("input[name='subId']").val(subId);
                  }
                });
              },
              "GET"
            );
          }

          $("#btnAdd").click(addTemplateDiv);

          if (data == undefined || (data && (data.eventType == "addSpec" || data.eventType == "addSpecFromSpecTable"))) {
            addTemplateDiv();
            $("#btnDel0").hide();
          }
          //data 非空取值names
          if (!config.isEmptyObject(data)) {
            data.name = data.name.indexOf("</span>") > 0 ? data.name.split("</span>")[1] : data.name;
            data.name =
              data.name.indexOf("&nbsp; &nbsp;") > 0
                ? data.name.split(" &nbsp; &nbsp;").length > 2
                  ? data.name.split(" &nbsp; &nbsp;")[2]
                  : data.name.split(" &nbsp; &nbsp;")[1]
                : data.name;
          }

          //添加子级渲染所属分类
          if (formId == "childForm") {
            data.code = "";
            data.name = "";
            data.description = "";
            data.status = "";
            // form.val(formId, data);
            $("form input[name='subId']").val(data.id);
            data = undefined;
          }

          function formatToTree(ary, pid, departmentId, pidName = "parentId") {
            return ary
              .filter((item) => item[pidName] === pid)
              .map((item) => {
                // 通过父节点ID递归查询所有子节点
                item.children = formatToTree(ary, item.id);
                item.value = item.id;
                item.selected = departmentId === item.id ? true : false;
                return item;
              });
          }
          function showSelectTree(treeData, element, roleItem, departmentId) {
            xmSelect.render({
              el: element,
              model: { label: { type: "text" } },
              radio: true,
              clickClose: true,
              filterable: true,
              tree: {
                show: true,
                strict: false,
                // expandedKeys: [-1],
                expandedKeys: false
              },
              tips: "请选择",
              // height: 'auto',
              data() {
                return formatToTree(treeData, "-1", departmentId);
              },
              on: function (data) {
                console.log(data);
                let depId = data.arr.length > 0 ? data.change[0].value : -1;
                $(element).siblings("input[name='departmentId']").val(depId);
                // 获取已经选取的岗位id
                var ids = "";
                $("select[name='roleId']").each(function (index, item) {
                  if ($(item).val() != null) {
                    if (ids == "") {
                      ids = $(item).val();
                    } else {
                      ids += "," + $(item).val();
                    }
                  }
                });
                // 获取岗位
                admin.req(
                  "user/admin/role/list",
                  {
                    limit: 9999,
                    departmentId: depId
                    // filterIds: ids
                  },
                  function (res) {
                    if (res.code == 200) {
                      var html = '<option value="">请选择责任岗位</option>';
                      for (var k = 0; k < res.data.length; k++) {
                        html += '<option value="' + res.data[k].id + '">' + res.data[k].name + "</option>";
                      }
                      roleItem.html(html);
                      form.render("select");
                    } else {
                      layer.msg("获取岗位失败", { icon: 2, time: 1000 });
                    }
                  },
                  "GET"
                );
              }
            });
          }

          function applyCompany(companyInfoItem, departmentItem, roleItem, length, del, companyInfoId, departmentId, roleId) {
            showSelectTree([], departmentItem, roleItem, departmentId);
            admin.req(
              "user/admin/companyInfo/list",
              { limit: 9999 },
              function (res) {
                if (res.code == 200) {
                  console.log(companyInfoId);
                  var html = '<option value="">请选择公司</option>';
                  for (var k = 0; k < res.data.length; k++) {
                    var elem = "";
                    if (companyInfoId && companyInfoId == res.data[k].id) {
                      elem = "selected";
                      console.log(222);
                    }
                    html += '<option value="' + res.data[k].id + '" ' + elem + " >" + res.data[k].name + "</option>";
                  }
                  companyInfoItem.html(html);
                  form.render("select");
                  form.on("select(getCompanyInfo" + length + ")", function (data) {
                    if (data.value) {
                      // 获取部门
                      admin.req(
                        "user/admin/department/list",
                        {
                          limit: 9999,
                          companyInfoId: data.value
                        },
                        function (res) {
                          if (res.code == 200) {
                            showSelectTree(res.data, departmentItem, roleItem, departmentId);
                          } else {
                            layer.msg("获取部门失败", { icon: 2, time: 1000 });
                          }
                        },
                        "GET"
                      );
                    } else {
                      roleItem.html("");
                      departmentItem.html("");
                      form.render("select");
                    }
                  });
                  form.val("systemMsgCategoryForm", { companyInfoId: JSON.parse(localStorage.getItem("wing")).login_user.companyInfoId });
                  admin.req(
                    "user/admin/department/list",
                    {
                      limit: 9999,
                      companyInfoId: JSON.parse(localStorage.getItem("wing")).login_user.companyInfoId
                    },
                    function (res) {
                      if (res.code == 200) {
                        showSelectTree(res.data, departmentItem, roleItem, departmentId);
                      } else {
                        layer.msg("获取部门失败", { icon: 2, time: 1000 });
                      }
                    },
                    "GET"
                  );
                  // 本地存储
                } else {
                  layer.msg("获取公司失败", { icon: 2, time: 1000 });
                }
              },
              "GET"
            );

            // if (companyInfoId) {
            //   // 获取部门
            //   admin.req(
            //     "user/admin/department/list",
            //     {
            //       limit: 9999,
            //       companyInfoId: companyInfoId
            //     },
            //     function (res) {
            //       if (res.code == 200) {
            //         showSelectTree(res.data, departmentItem, roleItem, departmentId);
            //       } else {
            //         layer.msg("获取部门失败", { icon: 2, time: 1000 });
            //       }
            //     },
            //     "GET"
            //   );
            // }

            if (departmentId) {
              // 获取岗位
              admin.req(
                "user/admin/role/list",
                {
                  limit: 9999,
                  departmentId: departmentId
                },
                function (res) {
                  if (res.code == 200) {
                    var html = '<option value="">请选择责任岗位</option>';
                    for (var k = 0; k < res.data.length; k++) {
                      var elem = "";
                      if (roleId && roleId == res.data[k].id) {
                        elem = "selected";
                      }
                      html += '<option value="' + res.data[k].id + '" ' + elem + " >" + res.data[k].name + "</option>";
                    }
                    roleItem.html(html);
                    form.render("select");
                  } else {
                    layer.msg("获取岗位失败", { icon: 2, time: 1000 });
                  }
                },
                "GET"
              );
            }

            if (del) {
              // 删除适用公司
              del.click(function () {
                $(this).parent().prev().prev().remove();
                $(this).parent().prev().remove();
                $(this).parent().remove();
              });
            }
          }

          if (data && data.id) {
            // 查询适用公司
            admin.req(
              "enterprise/sell/admin/customerDueSubjectCompany/list",
              {
                limit: 9999,
                subjectId: data.id
              },
              function (res) {
                if (res.code == 200) {
                  var html = "";
                  for (var j = 0; j < res.data.length; j++) {
                    if (j == 0) {
                      html += '<label class="layui-form-label"><span style="color: red">*</span>适用公司</label>';
                      html +=
                        '<div class="layui-input-block">\n' +
                        '                                    <input name="selId" type="hidden" value="' +
                        res.data[j].id +
                        '" />\n' +
                        '                                    <select name="companyInfoId" type="text" class="layui-input" lay-filter="getCompanyInfo" lay-verify="required" required ></select>\n' +
                        '                <input name="departmentId" type="hidden" />\n' +
                        "                <div style='float: left;width: 30%;margin-right: 10px;' id=\"departmentId" +
                        0 +
                        '"></div>\n' +
                        '                                <select name="roleId" type="text" class="layui-input" lay-verify="required" required ></select>\n' +
                        "                                </div>";
                    } else {
                      html +=
                        '<div style="height: 15px;"></div>\n' +
                        '            <label class="layui-form-label"></label>\n' +
                        '            <div class="layui-input-block">\n' +
                        '                <input name="selId" type="hidden" value=\'' +
                        res.data[j].id +
                        "' />\n" +
                        '                <select name="companyInfoId" type="text" class="layui-input" lay-filter="getCompanyInfo' +
                        j +
                        '" lay-verify="required" required ></select>\n' +
                        '                <input name="departmentId" type="hidden" />\n' +
                        "                <div style='float: left;width: 30%;margin-right: 10px;' id=\"departmentId" +
                        j +
                        '"></div>\n' +
                        '                <select name="roleId" type="text" class="layui-input" lay-verify="required" required ></select>\n' +
                        '                <i class="layui-icon layui-icon-delete icon-del" title="删除这一行"></i>' +
                        "            </div>";
                    }
                  }
                  $("#applyCompany").html(html);
                  form.render();
                  $("#applyCompany")
                    .find(".layui-input-block")
                    .each(function (index, item) {
                      var companyInfoId = res.data[index].companyInfoId;
                      var departmentId = res.data[index].departmentId;
                      var roleId = res.data[index].roleId;
                      if (index == 0) {
                        applyCompany(
                          $(item).find("select[name='companyInfoId']"),
                          "#departmentId0",
                          $(item).find("select[name='roleId']"),
                          "",
                          false,
                          companyInfoId,
                          departmentId,
                          roleId
                        );
                      } else {
                        applyCompany(
                          $(item).find("select[name='companyInfoId']"),
                          `#departmentId${index}`,
                          $(item).find("select[name='roleId']"),
                          index,
                          $(item).find("i.icon-del"),
                          companyInfoId,
                          departmentId,
                          roleId
                        );
                      }
                    });
                } else {
                  layer.msg(res.msg, { icon: 2 });
                }
              },
              "GET"
            );
          } else {
            applyCompany($("select[name='companyInfoId']"), "#departmentId0", $("select[name='roleId']"), "");
          }

          // 添加企业
          $("#addCompany").click(function () {
            var length = $("#applyCompany").find(".layui-input-block").length;
            var html =
              '<div style="height: 15px;"></div>\n' +
              '            <label class="layui-form-label"></label>\n' +
              '            <div class="layui-input-block">\n' +
              '                <select name="companyInfoId" type="text" class="layui-input" lay-filter="getCompanyInfo' +
              length +
              '" lay-verify="required" required ></select>\n' +
              '                <input name="departmentId" type="hidden" />\n' +
              "                <div style='float: left;width: 30%;margin-right: 10px;' id=\"departmentId" +
              length +
              '"></div>\n' +
              '                <select name="roleId" type="text" class="layui-input" lay-verify="required" required ></select>\n' +
              '                <i class="layui-icon layui-icon-delete icon-del" title="删除这一行"></i>' +
              "            </div>";
            $("#applyCompany").append(html);
            form.render();
            $("#applyCompany")
              .find(".layui-input-block")
              .each(function (index, item) {
                if (index == length) {
                  applyCompany($(item).find("select[name='companyInfoId']"), `#departmentId${length}`, $(item).find("select[name='roleId']"), length, $(item).find("i.icon-del"));
                }
              });
          });

          form.val("systemMsgCategoryForm", data);
          if (data !== undefined && data !== null) {
            var paramValues = data.paramValue.split(",");
            paramValues.forEach(function (value) {
              addTemplateDiv(value, true);
            });
          }

          if (data && data.customerType) {
            if (data.customerType.indexOf(",") != -1) {
              var types = data.customerType.split(",");
              $("input[name='customerType']").each(function (index, item) {
                if (types.indexOf($(item).val()) != -1) {
                  $(item).attr("checked", true);
                } else {
                  $(item).removeAttr("checked");
                }
              });
            } else {
              $("input[name='customerType']").each(function (index, item) {
                if ($(item).val() == data.customerType) {
                  $(item).attr("checked", true);
                } else {
                  $(item).ischecked = false;
                  $(item).removeAttr("checked");
                }
              });
            }
          }

          // 表单提交事件
          form.on("submit(" + submitButton + ")", function (d) {
            layer.load(2);
            if (d.field.subId == null || d.field.subId == "") {
              d.field.subId = -1;
            }
            var isDeleted = $("form input[name='isDeleted']").prop("checked");
            if (!isDeleted) {
              d.field.isDeleted = false;
            } else {
              d.field.isDeleted = true;
            }
            var paramValue = [];

            $("form input[name='paramValue']").each(function () {
              paramValue.push($(this).val());
            });

            d.field.paramValue = paramValue.toString();

            var customerDueSubjectCompanyList = [];
            $("#applyCompany")
              .find(".layui-input-block")
              .each(function (index, item) {
                var customerDueSubjectCompany = {};
                customerDueSubjectCompany.id = $(item).find("input[name='selId']").val();
                customerDueSubjectCompany.companyInfoId = $(item).find("select[name='companyInfoId']").val();
                customerDueSubjectCompany.departmentId = $(item).find("input[name='departmentId']").val();
                customerDueSubjectCompany.roleId = $(item).find("select[name='roleId']").val();
                customerDueSubjectCompanyList.push(customerDueSubjectCompany);
              });
            d.field.customerDueSubjectCompanyList = customerDueSubjectCompanyList;
            var customerTypes = "";
            $("input[name='customerType']:checked").each(function (index, item) {
              if (index == 0) {
                customerTypes = $(item).val();
              } else {
                customerTypes += "," + $(item).val();
              }
            });
            d.field.customerType = customerTypes;
            admin.req(
              data ? "enterprise/sell/admin/customerDueSubject/update" : "enterprise/sell/admin/customerDueSubject/add",
              JSON.stringify(d.field),
              function (res) {
                layer.closeAll("loading");
                if (res.code == 200) {
                  layer.msg(res.msg, { icon: 1 });
                  insTb.reload("systemMsgCategoryTable");
                  layer.closeAll("page");
                } else {
                  layer.msg(res.msg, { icon: 2 });
                }
              },
              data ? "PUT" : "POST",
              true,
              "application/json; charset=utf-8"
            );
            return false;
          });
        }
      });
    }

    // 显示详情弹窗
    function showViewModel(data) {
      admin.open({
        type: 1,
        area: "60%",
        offset: ["180px", "450px"],
        title: "查看责任岗位",
        content: $("#jobsView").html(),
        success: function () {
          // 渲染表格
          var insTb = table.render({
            id: "#jobsViewTable",
            elem: "#jobsViewTable",
            url: config.base + "enterprise/sell/admin/customerDueSubjectCompany/getRoleData",
            where: { subjectId: data.id },
            page: false,
            cellMinWidth: 100,
            cols: [
              [
                { type: "numbers" },
                { field: "companyInfoName", align: "center", title: "公司" },
                { field: "departmentName", align: "center", title: "部门" },
                { field: "name", align: "center", title: "岗位" }
              ]
            ]
          });
        }
      });
    }

    // 删除
    function doDelete(obj) {
      layer.confirm(
        "确定要删除吗？",
        {
          offset: "65px",
          skin: "layui-layer-admin"
        },
        function (i) {
          layer.close(i);
          layer.load(2);
          admin.req(
            "enterprise/sell/admin/customerDueSubject/" + obj.data.id,
            {},
            function (res) {
              layer.closeAll("loading");
              if (res.code == 200) {
                layer.msg(res.msg, { icon: 1 });
                obj.del();
                insTb.reload("systemMsgCategoryTable");
              } else {
                layer.msg(res.msg, { icon: 2 });
              }
            },
            "DELETE"
          );
        }
      );
    }

    // 导出excel
    $("#productCategoryBtnExport").click(function () {
      var checkRows = table.checkStatus("systemMsgCategoryTable");
      if (checkRows.data.length == 0) {
        layer.msg("请选择要导出的数据", { icon: 2 });
      } else {
        table.exportFile(insTb.config.id, checkRows.data, "xls");
      }
    });
  });
</script>
